//
//  SettingViewController.m
//  one·一个  tyw
//
//  Created by tyw on 15/9/9.
//  Copyright (c) 2015年 tyw. All rights reserved.
//

#import "SettingViewController.h"
#import "DayNightManager.h"
#import "DayNightLabel.h"
#import "FeedBackViewController.h"
#import "DataService.h"

@interface SettingViewController () <UITableViewDelegate,UITableViewDataSource>

@end

@implementation SettingViewController {
    UITableView* _tableView;
    
    DayNightManager* _manager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.titleView = nil;
    self.navigationItem.title = @"设置";
    
    [self _createTableView];
}

- (void)_createTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
    [self.view addSubview:_tableView];
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
}


#pragma mark - tableViewMethods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {         //浏览设置
        return 1;
    } else if (section == 1) {  //缓存设置
        return 1;
    } else if (section == 2) {      //更多
        return 4;
    } else {                    //退出当前账号
        return 1;
    }
}

//组头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    view.backgroundColor = [UIColor colorWithRed:105.0/255 green:105.0/255 blue:105.0/255 alpha:0.1];
    
    DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-50, 30)];
    [label loadLabelColor];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:15];
    [view addSubview:label];
    
    if (section == 0) {         //浏览设置
        label.text = @"浏览设置";
    } else if (section == 1) {  //缓存设置
        label.text = @"缓存设置";
    } else if (section == 2) {      //更多
        label.text = @"更多";
    } else {                    //退出当前账号
        
    }
    
    return view;
}
//组头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* cell = [[UITableViewCell alloc] init];
    cell.backgroundColor = [UIColor clearColor];
    
    
    if (indexPath.section == 0) {         //浏览设置
        DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
        [label loadLabelColor];
        label.text = @"夜间模式切换";
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:18];
        [cell.contentView addSubview:label];
        
        if (_manager == nil) {
            _manager = [DayNightManager sharedManager];
        }
        UISwitch* switchNight = [[UISwitch alloc] initWithFrame:CGRectMake(kWidth-71, 4.5, 51, 31)];
        NSString* type = [_manager getCurrentDayNightType];
        if ([type isEqualToString:kDayNightCurrentTypeIsDay]) {     //如果当前是白天
            switchNight.on = NO;
        } else {
            switchNight.on = YES;
        }
        [switchNight addTarget:self action:@selector(switchDayNightAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:switchNight];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    } else if (indexPath.section == 1) {  //缓存设置
        DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
        [label loadLabelColor];
        label.text = @"清除缓存";
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:18];
        [cell.contentView addSubview:label];
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
    } else if (indexPath.section == 2) {      //更多
        if (indexPath.row == 0) {       //去评分
            DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
            [label loadLabelColor];
            label.text = @"去评分";
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:18];
            [cell.contentView addSubview:label];
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        } else if (indexPath.row == 1) {        //反馈
            DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
            [label loadLabelColor];
            label.text = @"反馈";
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:18];
            [cell.contentView addSubview:label];
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        } else if (indexPath.row == 2) {        //用户协议
            DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
            [label loadLabelColor];
            label.text = @"用户协议";
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:18];
            [cell.contentView addSubview:label];
            
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            
        } else {                                //版本号
            DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(20, 0, kWidth-100, 40)];
            [label loadLabelColor];
            label.text = @"版本号";
            label.textAlignment = NSTextAlignmentLeft;
            label.font = [UIFont systemFontOfSize:18];
            [cell.contentView addSubview:label];
            
            DayNightLabel* versionNumLabel = [[DayNightLabel alloc] initWithFrame:CGRectMake(kWidth-50, 0, 40, 40)];
            [versionNumLabel loadLabelColor];
            versionNumLabel.text = @"2.4";
            versionNumLabel.textAlignment = NSTextAlignmentRight;
            versionNumLabel.font = [UIFont systemFontOfSize:16];
            [cell.contentView addSubview:versionNumLabel];
            
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

    } else {                    //退出当前账号
        DayNightLabel* label = [[DayNightLabel alloc] initWithFrame:CGRectMake(kWidth/2-100, 0, 200, 40)];
        [label loadLabelColor];
        label.text = @"退出当前账号";
        label.textAlignment = NSTextAlignmentCenter;
        label.font = [UIFont systemFontOfSize:18];
        [cell.contentView addSubview:label];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        
    if (indexPath.section == 1) {               //清除缓存
        UIActionSheet* clearSheet = [[UIActionSheet alloc] initWithTitle:@"清除缓存" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [clearSheet showInView:self.view];
    } else if (indexPath.section == 2) {        //更多
        if (indexPath.row == 0) {       //去评分
            NSLog(@"去评分");
        } else if (indexPath.row == 1) {//反馈
            FeedBackViewController* feedBackVC = [[FeedBackViewController alloc] init];
            [self.navigationController pushViewController:feedBackVC animated:YES];
        } else if (indexPath.row == 2) {//用户协议
            //获取用户协议时报错如下
            //Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x7fc41420a130 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set., NSUnderlyingError=0x7fc414209730 "Request failed: not found (404)"}
            
            
//            NSDictionary* params = @{@"from" : @"ONEApp"};
//            [DataService oneRequestUrl:kPolicyUrl httpMethod:@"GET" params:[params mutableCopy] block:^(id result) {
//                if ([result isKindOfClass:[NSError class]]) {
//                    NSLog(@"用户协议获取失败  %@", result);
//                }
//                
//                
//                
//            }];
            
        }
    } else if (indexPath.section == 3){                                    //退出当前账号
        UIActionSheet* logOutSheet = [[UIActionSheet alloc] initWithTitle:@"退出后依然可以正常浏览内容，如需收藏需要重新登录" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [logOutSheet showInView:self.view];
    }
}

#pragma mark - actions
- (void)switchDayNightAction:(UISwitch*)switchNight {
    if (switchNight.on) {       //切换到夜间模式
        [_manager changeCurrenDayNightType:kDayNightCurrentTypeIsNight];
        [self changeDayNightShow:kDayNightCurrentTypeIsNight];
    } else {
        [_manager changeCurrenDayNightType:kDayNightCurrentTypeIsDay];
        [self changeDayNightShow:kDayNightCurrentTypeIsDay];
    }
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if ([actionSheet.title isEqualToString:@"清除"]) {
        if (buttonIndex == 0) {             //确定
            NSLog(@"清除缓存");
            
        } else if (buttonIndex == 1) {      //取消
            NSLog(@"取消");
        }
    } else {
        if (buttonIndex == 0) {             //确定
            NSLog(@"登出当前账号");
        } else if (buttonIndex == 1) {      //取消
            NSLog(@"取消");
        }
    }
    
}

@end
